﻿///
/// Pamela.Logic.Imps
///
namespace Pamela.Logic.Imps.Mvc
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;

    using Pamela.Logic.Helpers;
    using Pamela.Logic.Imps.Base;    
    using Pamela.Logic.Properties;

    /// <summary>
    /// RepositoryBuilder class
    /// </summary>
     class MvcRepositoryBuilder : BaseBuilder
    {
        /// <summary>
        /// Initializes a new instance of the RepositoryBuilder class
        /// </summary>
        /// <param name="assembly">The shared DLL assembly</param>
        /// <param name="info">Builder  object containing all the info to necessary perform the scaffolding operation</param>
         public MvcRepositoryBuilder(Assembly assembly, BuilderInfo info, string layerName)
             : base(assembly, info, layerName)
        {
        }

        /// <summary>
        /// Builds the data layeer files based on the BuidlerInfo provided
        /// </summary>
        public override void Build()
        {
            GenerateContextCode();
            GenerateRepositoryCode();
        }

        /// <summary>
        /// Builds the repository file
        /// </summary>
        private void GenerateRepositoryCode()
        {
            var source = SourceDirectory + "repository.template";
            var target = this.builderInfo.SolutionInnerDirectory + "\\" + builderInfo.RootNamespace + "." + this.layerName + "\\Repositories\\" + this.builderInfo.SelectedModel + "Repository.cs";

            using (StreamReader template = new StreamReader(source))
            using (StreamWriter output = new StreamWriter(target))
            {
                while (template.Peek() >= 0)
                {
                    string buffer = template.ReadLine();
                    buffer = buffer.Replace(Resources.NamespaceDal, this.builderInfo.DataLayerNamespace);
                    buffer = buffer.Replace(Resources.Namespace, this.builderInfo.RootNamespace);
                    buffer = buffer.Replace(Resources.SelectedModel, this.builderInfo.SelectedModel);
                    output.WriteLine(buffer);
                }
            }        
        }

        /// <summary>
        /// Builds the partial database context file
        /// </summary>
        public void GenerateContextCode()
        {
            string source = SourceDirectory + "partialdbcontext.template";
            string target = this.builderInfo.SolutionInnerDirectory + "\\" + builderInfo.RootNamespace + "." + this.layerName + "\\Context\\DatabaseContext." + this.builderInfo.SelectedModel + ".cs";

            using (StreamReader template = new StreamReader(source))
            using (StreamWriter output = new StreamWriter(target))
            {
                while (template.Peek() >= 0)
                {
                    string buffer = template.ReadLine();
                    buffer = buffer.Replace(Resources.Entity, pluralizationService.Pluralize(this.builderInfo.SelectedModel));
                    buffer = buffer.Replace(Resources.Namespace, this.builderInfo.RootNamespace);
                    buffer = buffer.Replace(Resources.SelectedModel, this.builderInfo.SelectedModel);
                    output.WriteLine(buffer);
                }
            }
        }
    }
}
